    package com.example.diyview.Viewgroup.Flow;

    import android.content.Context;
    import android.content.res.TypedArray;
    import android.icu.util.Measure;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.TextView;

    import com.example.diyview.R;
    import com.example.diyview.Utils.SizeUtils;

    import java.util.ArrayList;
    import java.util.List;

    public class FlowLayout extends ViewGroup {
    //定义行的默认值
    public static final int DEFAULT_LINE = SizeUtils.dp2px(5f);
    //定义横向子项间距。现在是px不适配需要后期转化
    public static final  int DEFAULT_HorizontalMargin=SizeUtils.dp2px(5f);
    public static final  int DEFAULT_VerticalMargin=SizeUtils.dp2px(5f);
    public static final  int DEFAULT_textmaxLength=SizeUtils.dp2px(5f);
    public static final int DEFAULT_BorderRadio=SizeUtils.dp2px(5f);
    public int maxLines;
    public float itemHorizontalMargin;
    public float ItemVerticalMargin;
    public int textmaxLength;
    public int textcolor;
    public int borderColor;
    public float BorderRadio;
    private List<String> mdata = new ArrayList<>();
    private static final String TAG = "FlowLayout";
        public FlowLayout(Context context) {
            this(context,null);
        }
        public FlowLayout(Context context, AttributeSet attrs) {
            this(context, attrs,0);
        }
        public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }

        public FlowLayout(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
            super(context, attrs, defStyleAttr, defStyleRes);
    //        获取属性
            TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.FlowLayout);
    //        maxLines
             maxLines = typedArray.getInt(R.styleable.FlowLayout_MaxLines, DEFAULT_LINE);
    //        itemHorizontalMargin
             itemHorizontalMargin = typedArray.getDimension(R.styleable.FlowLayout_itemHorizontalMargin, DEFAULT_HorizontalMargin);
    //        ItemVerticalMargin
             ItemVerticalMargin = typedArray.getDimension(R.styleable.FlowLayout_ItemVerticalMargin,DEFAULT_VerticalMargin);
    //        textmaxLength
             textmaxLength = typedArray.getInt(R.styleable.FlowLayout_textmaxLength, DEFAULT_textmaxLength);
    //        textcolor
             textcolor = typedArray.getColor(R.styleable.FlowLayout_textcolor, getResources().getColor(R.color.text_grey));
    //        borderColor
             borderColor = typedArray.getColor(R.styleable.FlowLayout_borderColor,  getResources().getColor(R.color.text_grey));
    //        BorderRadio
             BorderRadio = typedArray.getDimension(R.styleable.FlowLayout_BorderRadio, DEFAULT_BorderRadio);
            Log.d(TAG, "FlowLayout:maxLines"+maxLines);
            Log.d(TAG, "FlowLayout:itemHorizontalMargin"+itemHorizontalMargin);
            Log.d(TAG, "FlowLayout:ItemVerticalMargin"+ItemVerticalMargin);
            Log.d(TAG, "FlowLayout:textmaxLength"+textmaxLength);
            Log.d(TAG, "FlowLayout:textcolor"+textcolor);
            Log.d(TAG, "FlowLayout:borderColor"+borderColor);
    //        回收掉
            typedArray.recycle();
        }
        public void settext(List<String> data){
            this.mdata.clear();
            this.mdata.addAll(data);
    //        根据数据创建子View并添加进来
            setupChildren();
        }

        private void setupChildren() {
    //        先清空所有view
            removeAllViews();
    //        再添加子View
            for (String mdatum : mdata) {
                TextView textView = (TextView) LayoutInflater.from(getContext()).inflate(R.layout.item_flow_text,this,false);
                textView.setText(mdatum);
    //            设置Textview的相关属性
                addView(textView);
            }
        }
    private List<List<View>> mlines = new ArrayList<>();
        /**
         * 这两个值来自父控件包含值和模式
         * @param widthMeasureSpec
         * @param heightMeasureSpec
         */
        @Override
        protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
            super.onMeasure(widthMeasureSpec, heightMeasureSpec);
            int mode = MeasureSpec.getMode(widthMeasureSpec);
            int mode1 = MeasureSpec.getMode(heightMeasureSpec);
            int size = MeasureSpec.getSize(widthMeasureSpec);
            int size1 = MeasureSpec.getSize(heightMeasureSpec);
    //        测量子view
            int childCount = getChildCount();
            if (childCount == 0){
                return;
            }
    //        先清空
            mlines.clear();
    //        添加默认行
            List<View>line =new ArrayList<>();
            mlines.add(line);
            int childWidthspace = MeasureSpec.makeMeasureSpec(size, MeasureSpec.AT_MOST);
            int childHeightspace = MeasureSpec.makeMeasureSpec(size1, MeasureSpec.AT_MOST);
            for (int i = 0; i <childCount ; i++) {
                View child = getChildAt(i);
    //            判读是否可以添加到当前行
                if (line.size() ==0){
    //可以添加
                    line.add(child);
                }else {
    //                判断是否可以添加到当前行
                    Boolean aBoolean = checkChildCanBeAdd(line, child, size);
                    if (aBoolean){
                        line.add(child);
                    }else {
                        line = new ArrayList<>();
                        mlines.add(line);
                    }
                }
    //            测量子view
                measureChild(child,childWidthspace,childHeightspace);
    //            拿到子view的尺寸
            }
    //            根据尺寸计算行
            View child = getChildAt(0);
            int measuredHeight = child.getMeasuredHeight();
            int parentHeightTargetSize = measuredHeight * mlines.size();
    //        测量自身
            setMeasuredDimension(size,parentHeightTargetSize);
        }

        private Boolean checkChildCanBeAdd(List<View>line,View child,int size) {
            int measuredWidth = child.getMeasuredWidth();
            int totalwidth =0;
            for (View view : line) {
               totalwidth+= view.getMeasuredWidth();
            }
            totalwidth +=measuredWidth;
    //        如果超出限制宽度责不可以在添加
    //        否则可以添加
            return totalwidth<=size;
        }

        @Override
        protected void onLayout(boolean changed, int l, int t, int r, int b) {
//            View childAt = getChildAt(0);
//            childAt.layout(0,0,childAt.getMeasuredWidth(),childAt.getMeasuredHeight());
//            View childAt1 = getChildAt(1);
//            childAt1.layout(childAt.getRight(),0,childAt.getRight()+childAt1.getMeasuredWidth(),childAt1.getMeasuredHeight());
           int currentLeft =0;
           int currentTop = 0;
           int currentBottom=0;
           int currentRight=0;
            for (List<View> mline : mlines) {
                for (View view : mline) {
//                    布局每一行
                    int Width = view.getMeasuredWidth();
                    int Height = view.getMeasuredHeight();
                    currentRight += Width;
                    currentBottom += Height;
                    view.layout(currentLeft,currentTop,currentRight,currentBottom);
                    currentLeft = currentRight;
                }
                currentLeft =0;
                currentBottom =getChildAt(0).getHeight();
                currentTop += getChildAt(0).getHeight();
            }
        }

        public int getMaxLines() {
            return maxLines;
        }

        public void setMaxLines(int maxLines) {
            this.maxLines = maxLines;
        }

        public float getItemHorizontalMargin() {
            return itemHorizontalMargin;
        }

        public void setItemHorizontalMargin(float itemHorizontalMargin) {
            this.itemHorizontalMargin = itemHorizontalMargin;
        }

        public float getItemVerticalMargin() {
            return ItemVerticalMargin;
        }

        public void setItemVerticalMargin(float itemVerticalMargin) {
            ItemVerticalMargin = itemVerticalMargin;
        }

        public int getTextmaxLength() {
            return textmaxLength;
        }

        public void setTextmaxLength(int textmaxLength) {
            this.textmaxLength = textmaxLength;
        }

        public int getTextcolor() {
            return textcolor;
        }

        public void setTextcolor(int textcolor) {
            this.textcolor = textcolor;
        }

        public int getBorderColor() {
            return borderColor;
        }

        public void setBorderColor(int borderColor) {
            this.borderColor = borderColor;
        }

        public float getBorderRadio() {
            return BorderRadio;
        }

        public void setBorderRadio(float borderRadio) {
            BorderRadio = borderRadio;
        }
    }
